package org.dragon.services.impls;

import jakarta.annotation.Resource;
import org.dragon.mappers.ConfigMapper;
import org.dragon.models.AppConfig;
import org.dragon.models.Config;
import org.dragon.services.ConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Author:     Zhao Yan
 * DateTime:   2023/5/23 9:57
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Resource
    private ConfigMapper configMapper;

    @Cacheable(value = "config", key = "'app'", sync = true)
    @Override
    public AppConfig getAppConfig() {
        Config app = configMapper.selectByPrimaryKey("app");
        System.out.println("Load config from database: " + app);
        return AppConfig.fromContent(app.getContent());
    }

    @CacheEvict(value = "config", key = "'app'")
    @Override
    public void saveAppConfig(AppConfig config) {
        Config c = new Config();
        c.setId("app");
        c.setContent(config.toContent());
        configMapper.updateByPrimaryKey(c);
    }
}
